package com.whf.android.jar.util.skip;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.app.Latte;
import com.whf.android.jar.dao.LocateDao;


import java.util.ArrayList;
import java.util.List;

/**
 * 导航软件选择工具类
 *
 * @author qf
 * @version 1.0.2
 */
public final class AMapStartUtils {

    //1.百度地图包名
    public static final String BAIDU_MAP_PACKAGE_NAME = "com.baidu.BaiduMap";
    //2.高德地图包名
    public static final String AUTO_NAVI_PACKAGE_NAME = "com.autonavi.minimap";
    //3.腾讯地图包名
    public static final String QQ_MAP_PACKAGE_NAME = "com.tencent.map";

    private final Navigation navigation;

    public static void init(Navigation navigation) {
        new AMapStartUtils(navigation);
    }

    public AMapStartUtils(Navigation navigation) {
        this.navigation = navigation;
        start();
    }

    /**
     * 检查手机上是否安装了指定的软件
     *
     * @return 目标软件中已安装的列表
     */
    @NonNull
    public static List<String> checkInstalledPackage() {
        final String[] MAP_PACKAGES = {BAIDU_MAP_PACKAGE_NAME, AUTO_NAVI_PACKAGE_NAME, QQ_MAP_PACKAGE_NAME};
        //获取所有已安装程序的包信息
        PackageManager manager = Latte.getApplicationContext().getPackageManager();
        //获取所有已安装程序的包信息
        @SuppressLint("QueryPermissionsNeeded")
        List<PackageInfo> installedPackages = manager.getInstalledPackages(0);
        //
        final List<String> check = new ArrayList<>();
        if (installedPackages != null) {
            for (PackageInfo info : installedPackages) {
                if (MAP_PACKAGES[0].equals(info.packageName)) {
                    check.add("百度地图");
                } else if (MAP_PACKAGES[1].equals(info.packageName)) {
                    check.add("高德地图");
                } else if (MAP_PACKAGES[2].equals(info.packageName)) {
                    check.add("腾讯地图");
                }
            }
        }
        return check;
    }

    public void start() {
        if (navigation == null) {
            ToastUtils.showShort("请选择目的位置。");
            return;
        }
        final List<String> check = checkInstalledPackage();
        if (check.size() == 0) {
            openBrowserToGuide();
            return;
        }
        new SkipPopup(checkInstalledPackage(), new SkipPopup.IClickListener() {
            @Override
            public void daiDu() {
                openBaiDuMapToGuide();
            }

            @Override
            public void gaoDe() {
                openGaoDeMapToGuide();
            }

            @Override
            public void tencent() {
                openTencentMapToGuide();
            }
        }).popup();
    }

    /**
     * 打开百度地图
     * https://lbs.amap.com/api/amap-mobile/guide/android/route
     */
    private void openBaiDuMapToGuide() {
//        i1.setData(Uri.parse("baidumap://map/direction
//        ?origin=name:对外经贸大学|latlng:39.98871,116.43234&destination=西直门&coord_type=bd09ll&src=andr.baidu.openAPIdemo"));
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);

        StringBuilder stringBuffer = new StringBuilder("baidumap://map/direction?destination=&mode=driving&coord_type=bd09ll");
        if (LocateDao.getLatitude() != 0.0 && LocateDao.getLatitude() != 0.0) {
            stringBuffer.append("&origin=").append("latlng:").append(LocateDao.getLatitude()).append(",").append(LocateDao.getLongitude());
        }
        stringBuffer.append("&destination=name:").append(navigation.getStoreName())
                .append("|latlng:").append(navigation.getLatitude()).append(",").append(navigation.getLongitude());
        stringBuffer.append("&src=").append(Latte.getApplicationContext().getPackageName());
        Uri uri = Uri.parse(stringBuffer.toString());
        //将功能Scheme以URI的方式传入data
        intent.setData(uri);
        //启动该页面即可
        ActivityUtils.startActivity(intent);
    }

    /**
     * 打开高德地图
     * https://lbs.amap.com/api/amap-mobile/guide/android/route
     */
    private void openGaoDeMapToGuide() {
//i1.setData(Uri.parse("amapuri://route/plan/
// ?sid=&slat=39.92848272&slon=116.39560823&sname=大西洋&did=&dlat=39.98848272&dlon=116.47560823&dname=太平洋&dev=0&t=0"));
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        StringBuilder stringBuffer = new StringBuilder("androidamap://route?sourceApplication=").append("amap");
        if (LocateDao.getLatitude() != 0.0 && LocateDao.getLatitude() != 0.0) {
            stringBuffer.append("&slat=").append(LocateDao.getLatitude()).append("&slon=").append(LocateDao.getLongitude());
        }
        stringBuffer.append("&dlat=").append(navigation.getLatitude()).append("&dlon=").append(navigation.getLongitude());
        stringBuffer.append("&dname=").append(navigation.getStoreName()).append("&dev=0&t=0");

        Uri uri = Uri.parse(stringBuffer.toString());
        //将功能Scheme以URI的方式传入data
        intent.setData(uri);
        //启动该页面即可
        ActivityUtils.startActivity(intent);
    }

    /**
     * 打开腾讯地图
     * https://lbs.amap.com/api/amap-mobile/guide/android/route
     */
    private void openTencentMapToGuide() {
// i1.setData(Uri.parse("qqmap://map/routeplan
// ?type=drive&from=清华&fromcoord=39.994745,116.247282&to=怡和世家&tocoord=39.867192,116.493187&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"));

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        StringBuilder stringBuffer = new StringBuilder("qqmap://map/routeplan?type=drive");
        if (LocateDao.getLatitude() != 0.0 && LocateDao.getLatitude() != 0.0) {
            stringBuffer.append("&forcoord=").append(LocateDao.getLatitude()).append(",").append(LocateDao.getLongitude());
        }
        stringBuffer.append("&tocoord=").append(navigation.getLatitudeBaiDu()).append(",").append(navigation.getLongitudeBaiDu());
        stringBuffer.append("&to=").append(navigation.getStoreName());
        stringBuffer.append("&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77");

        Uri uri = Uri.parse(stringBuffer.toString());
        //将功能Scheme以URI的方式传入data
        intent.setData(uri);
        //启动该页面即可
        ActivityUtils.startActivity(intent);
    }

    /**
     * 打开浏览器
     */
    private void openBrowserToGuide() {
        String url = "http://uri.amap.com/navigation?to=" + navigation.getLongitude() + "," + navigation.getLatitude() + "," +
                navigation.getStoreName() + "&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0";
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ActivityUtils.startActivity(intent);
    }
}
